﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;


public class GZipCompresser : Singleton<GZipCompresser>
{
    /// <summary>
    /// ZIP压缩
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] Compress(byte[] data)
    {
        MemoryStream ms = new MemoryStream();
        GZipOutputStream compressedStream = new GZipOutputStream(ms);

        MemoryStream inputBuffer = new MemoryStream(data);
        byte[] block = new byte[1024];
        while (true)
        {
            int bytesRead = inputBuffer.Read(block, 0, block.Length);
            if (bytesRead <= 0)
            {
                inputBuffer.Close();
                break;
            }
            else
                compressedStream.Write(block, 0, bytesRead);
        }

        compressedStream.Finish();
        compressedStream.Close();
        ms.Close();
        return ms.ToArray();
    }

    public static ThreadJob CompressAsync(byte[] zippedData)
    {
        return AppFacade.threadManager.SetJob(() =>
        {
            return Compress(zippedData);
        });
    }
}
